package hu.genius.view;

import hu.genius.CoreApplication;
import hu.genius.R;
import hu.genius.model.BasketManager;
import hu.genius.model.entity.Product;
import hu.genius.view.utils.ViewUtils;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SelectProduct extends Activity implements OnClickListener {
	private static final int DEFAULT_QUANTITY = 1;
	
	private BasketManager basketManager;
	private Product product;
	private int quantity = DEFAULT_QUANTITY;
	
	private TextView quantityTextView;
	private TextView finalPriceTextView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectproduct);
		
		basketManager = ((CoreApplication) this.getApplication()).getBasketManager();
		product = getProductFromIndent();
		
		int oldQuantity = basketManager.getQuantityByProductId(product);
		Log.d("zs", "old quantity: " + oldQuantity);
		if (oldQuantity > 0) {
			quantity = oldQuantity;
		}
		
		ImageView productView = (ImageView) findViewById(R.id.productImage);
		Bitmap bitmap = null;
		if (product.getImage() != null) {
			bitmap = ViewUtils.downloadImage(product.getImage());
		} else {
			bitmap = ViewUtils.loadImage(this, product.getId()	+ "s110c" + product.getPrice() + ".00i");
		}
		productView.setImageBitmap(bitmap);
		productView.setLayoutParams(new LinearLayout.LayoutParams(
				ViewUtils.getDpSize(this, 110), ViewUtils.getDpSize(this, 110)));
		
		TextView productNameTextView = (TextView) findViewById(R.id.productNameLabel);
		productNameTextView.setText(product.getName());
		
		TextView productPriceTextView = (TextView) findViewById(R.id.productPriceLabel);
		productPriceTextView.setText(String.valueOf(product.getPrice()) + " Ft");
		
		quantityTextView = (TextView) findViewById(R.id.quantityLabel);
		finalPriceTextView = (TextView) findViewById(R.id.finalPriceLabel);
		
		Button plusButton = (Button) findViewById(R.id.plusButton);
		plusButton.setOnClickListener(this);
		
		Button minusButton = (Button) findViewById(R.id.minusButton);
		minusButton.setOnClickListener(this);

		Button backButton = (Button) findViewById(R.id.selectedBackButton);
		backButton.setOnClickListener(this);
		
		Button checkOutButton = (Button) findViewById(R.id.addBasketButton);
		checkOutButton.setOnClickListener(this);
		
		updateUI();
	}
	
	public void back(View view) {
		this.finish();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.selectedBackButton: {
			this.finish();
			break;
		}
		case R.id.addBasketButton: {
			if (product != null) {
				basketManager.setProductByQuantity(product, quantity);
			}
			Intent refresh = new Intent(this, BasketActivity.class);
			startActivity(refresh);
			this.product = null;
			this.quantity = DEFAULT_QUANTITY;
			SelectProduct.this.finish();
			break;
		}
		case R.id.plusButton: {
			++quantity;
			updateUI();
			break;
		}
		case R.id.minusButton: {
			if (quantity > 0) {
				--quantity;
				updateUI();
			}
			break;
		}
		}
	}
	
	private Product getProductFromIndent() {
		Log.d("zs", "getting intent parameters...");
		Bundle bundle = this.getIntent().getExtras();
		Product product = new Product();
		if (bundle != null) {
			product.setId(bundle.getInt("product_id"));
			product.setCategoryId(bundle.getInt("product_category_id"));
			product.setName(bundle.getString("product_name"));
			product.setPrice(bundle.getInt("product_price"));
			product.setImage(bundle.getString("image"));
		}
		product = getSameProduct(product);
		Log.d("zs", "Product from Intent: " + product);
		return product;
	}
	
	/**
	 * Megnézi, hogy van-e már a kosárban ilyen termék. Azért kell, mert nem
	 * egy termék példányt kap, hanem egy termék adatait és ezért a kosárban levő
	 * termék és az új termék nem egy példány lesz. /Hack/
	 * 
	 * @param product a kiválasztott termék
	 * @return ha a kosárban van ilyen termék, akkor az a példány, egyébként a
	 * kapottal tér vissza.
	 */
	private Product getSameProduct(Product product) {
		for (Product prod : this.basketManager.getOrderMap().keySet()) {
			if (prod.getId() == product.getId()) {
				Log.d("zs", "found product in basket: " + prod);
				return prod;
			}
		}
		return product;
	}
	
	private void updateUI() {
		Log.d("zs", "update UI - " + quantity);
		quantityTextView.setText(quantity + " db");
		finalPriceTextView.setText(String.valueOf(quantity * product.getPrice()) + " Ft");
	}
}
